package com.framework.controller.system;

import java.util.HashMap;
import java.util.Set;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.framework.controller.index.BaseController;
import com.framework.entity.LogSMSFormMap;
import com.framework.util.CCPRestSDK;
import com.framework.util.Common;
import com.framework.util.datasource.EnvironmentFileInfo;

/**
 * 本质：控制型逻辑，拥有强大的权利与义务 信息的拦截与处理
 * 
 * @author 李品良:
 * @version 创建时间：2016年8月15日 下午3:12:27 类说明
 */
@Controller
@RequestMapping("/interface_office/")
public class InterfaceOfficeController extends BaseController {

	@ResponseBody
	@RequestMapping("saveBaidusem")
	public String saveBaidusem() throws Exception {
		String SMS_MODE = EnvironmentFileInfo.createdb.get("SMS_MODE").toString();
		LogSMSFormMap logSMSFormMap = getMapformDataMap(LogSMSFormMap.class, getMapFromRequest());
		String username = getPara("username");
		String enterprise = getPara("enterprise");
		String cellphone = getPara("cellphone");
		String qq = getPara("qq");

		if (!Common.isLengthLegal(username) && !Common.isLengthLegal(enterprise) && !Common.isLengthLegal(cellphone)) {
			System.out.println(username + "=========" + enterprise + "===========" + cellphone);
			return "输入字符的长度有错误";
		}

		if (!Common.isCellphone(cellphone)) {
			return "手机号码输入错误";
		}
		logSMSFormMap.put("userIP", getIpAddr());
		logSMSFormMap.put("username", username);
		logSMSFormMap.put("enterprise", enterprise);
		logSMSFormMap.put("cellphone", cellphone);
		logSMSFormMap.put("qq", qq);
		logSMSFormMap.save();

		if (SMS_MODE.equals("1")) {
			sms(cellphone, enterprise, username, qq);
		}

		return "success";

	}

	
	@ResponseBody
	@RequestMapping("saveHow")
	public String saveHow() throws Exception {
		String SMS_MODE = EnvironmentFileInfo.createdb.get("SMS_MODE").toString();
		LogSMSFormMap logSMSFormMap = getMapformDataMap(LogSMSFormMap.class, getMapFromRequest());
		String username = getPara("username");
		String cellphone = getPara("cellphone");
		String qq = getPara("qq");

		if (!Common.isLengthLegal(username) && !Common.isLengthLegal(cellphone)) {
			System.out.println(username  + "===========" + cellphone);
			return "输入字符的长度有错误";
		}

		if (!Common.isCellphone(cellphone)) {
			return "手机号码输入错误";
		}

		logSMSFormMap.put("userIP", getIpAddr());
		logSMSFormMap.put("username", username);
		logSMSFormMap.put("cellphone", cellphone);
		logSMSFormMap.put("qq", qq);
		logSMSFormMap.save();

		if (SMS_MODE.equals("1")) {
			sms(cellphone, cellphone, username, cellphone);
		}

		return "success";
	}
	
	// 13922264656
	public static void sms(String tel, String enterprise, String userName, String qq) {
		HashMap<String, Object> result = null;
		CCPRestSDK restAPI = new CCPRestSDK();
		restAPI.init("app.cloopen.com", "8883");// 初始化服务器地址和端口，格式如下，服务器地址不需要写https://
		restAPI.setAccount("aaf98f893f6a6cb3013f7214530e006f", "460288375d22403f950b298254f952b9");// 初始化主帐号和主帐号TOKEN
		restAPI.setAppId("8aaf070856bbb3c50156bbb85430000d");// 初始化应用ID
		// new String[]{randStr,"3"} randStr是验证码，第二个是有效期
		result = restAPI.sendTemplateSMS("13922264656", "130294", new String[] { tel, enterprise, userName, qq });
		System.out.println("SDKTestSendTemplateSMS result=" + result);
		if ("000000".equals(result.get("statusCode"))) {
			// 正常返回输出data包体信息（map）
			HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
			Set<String> keySet = data.keySet();
			for (String key : keySet) {
				Object object = data.get(key);
				System.out.println(key + " = " + object);
			}
		} else {
			// 异常返回输出错误码和错误信息
			System.out.println("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
		}
	}
}
